<script setup>
import { ref } from 'vue'
const empPwd = ref({
  "old_pwd": '',
  "new_pwd": '',
  "re_pwd": ''
})
//自定义确认密码的校验函数
const rePasswordValid = (rule, value, callback) => {
  if (value === null || value === '') {
    return callback(new Error('请再次确认密码'))
  }
  if (empPwd.value.new_pwd !== value) {
    return callback(new Error('两次输入密码不一致'))
  }
}

//用于注册的表单校验模型
const updatePassword = ref({
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 5, max: 16, message: '密码长度必须位5到16位', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 5, max: 16, message: '密码长度必须位5到16位', trigger: 'blur' }
  ],
  re_pwd: [
    { validator: rePasswordValid, trigger: 'blur' }
  ]
})

import {updateEmpPwdService} from '@/api/Emp.js'
import { ElMessage } from 'element-plus'
import {useRouter} from 'vue-router'
const router = useRouter()
const updatePwd = async()=>{
  let result = await updateEmpPwdService(empPwd.value)
  ElMessage.success(result.message?result.massage:'修改成功');
  router.push('/login')
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>修改密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form :model="empPwd" :rules="updatePassword" label-width="100px" size="large">
          <el-form-item label="原密码" prop="old_pwd">
            <el-input v-model="empPwd.old_pwd"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input v-model="empPwd.new_pwd"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input v-model="empPwd.re_pwd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updatePwd">提交修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>